from setting import Setting
import data
import os
import numpy as np
import matplotlib.pyplot as plt
import keras
from keras import backend
from keras import layers
from keras import models
from keras.models import load_model
import tool

def predict_(set, i0, i1, i2, i3, i4, i5, i6):
  # 获取文件夹地址
  path = os.path.split(os.path.realpath(__file__))[0]
  # 加载整个模型
  model = load_model('{}/{}/{}_{}_{}_{}_{}_{}_{}.h5'.format(path,set.CHECKPOINTS_PATH,i0,i1, i2, i3, i4, i5, i6))
  # 获取数据
  train_x, train_y, test_x, test_y, predict_x, predict_y = data.getDate(set, False)
  # 开始预测
  predicts = model.predict(predict_x, batch_size=1)
  # 除之前的模型.
  backend.clear_session()
  res = tool.Statistics(predicts, predict_y,fileName="{}_{}_{}_{}_{}_{}_{}".format(i0, i1, i2, i3, i4, i5, i6))
  tool.savaData(set,res)

def trainPredict_(set, i0, i1, i2, i3, i4, i5, i6):
  # 获取文件夹地址
  path = os.path.split(os.path.realpath(__file__))[0]
  # 加载整个模型
  model = load_model('{}/{}/{}_{}_{}_{}_{}_{}_{}.h5'.format(path,set.CHECKPOINTS_PATH,i0,i1, i2, i3, i4, i5, i6))
  # 获取数据
  data_ = data.getDateFromExeQuery(set, True)
  predict_x = data_["predict_x"]
  predict_y = data_["predict_y"]
  # 开始预测
  predicts = model.predict(predict_x, batch_size=1)
  # 除之前的模型.
  backend.clear_session()
  res = tool.Statistics(predicts, predict_y,fileName="{}_{}_{}_{}_{}_{}_{}".format(i0, i1, i2, i3, i4, i5, i6))
  tool.savaData(set,res)

def getFiles(path):
  files = os.listdir(path)
  return files

def testPredict():
  set = Setting()
  # 获取文件夹地址
  path = os.path.split(os.path.realpath(__file__))[0]
  getFilesList = getFiles(path + "/" + set.CHECKPOINTS_PATH)
  # getFilesList = ["a00317__60_2_16_0.6_16.h5"]
  for item in range(0, len(getFilesList)):
    item = getFilesList[item]
    if item.split(".")[-1:][0] == "png":
      continue
    item = item[:-3]
    sl = item.split("_")
    s = []
    for _ in sl:
      if _: s.append(_)
    sl = s
    sl[0] = sl[0] + "_"
    for i in range(1, len(sl)):
      if i == 4:
        sl[i] = float(sl[i])
        continue
      if i == 6:
        sl[i] = float(sl[i])
        continue
      sl[i] = int(sl[i])
    set.setDef(EPOCHS_=sl[1], BATCH_SIZE_=sl[2], MAX_STEPS_=sl[3], DROPOUT_RATE_=sl[4], LSTM_UNITS_=sl[5])
    predict_(set,sl[0],sl[1],sl[2],sl[3],sl[4],sl[5],sl[6])
  print("======================================================================================")
  print("======================================================================================")
  print("=================================    验证完成    =====================================")
  print("======================================================================================")
  print("======================================================================================")



if __name__ == '__main__':
  testPredict()

  # sl = ["b0005", 30, 25, 30, 0.6, 32]
  # set.setDef(EPOCHS_=sl[1], BATCH_SIZE_=sl[2], MAX_STEPS_=sl[3], DROPOUT_RATE_=sl[4], LSTM_UNITS_=sl[5])
  # predict_(set,sl[0],sl[1],sl[2],sl[3],sl[4],sl[5],sl[6])
